package com.lengtouzai.databaseexp.view.personal;


import com.lengtouzai.databaseexp.entity.User;
import com.lengtouzai.databaseexp.view.main.MainView;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.router.Route;
import io.jmix.core.DataManager;
import io.jmix.core.security.CurrentAuthentication;
import io.jmix.flowui.Notifications;
import io.jmix.flowui.ViewNavigators;
import io.jmix.flowui.kit.component.button.JmixButton;
import io.jmix.flowui.model.InstanceContainer;
import io.jmix.flowui.view.*;
import org.springframework.beans.factory.annotation.Autowired;

@Route(value = "personal-view", layout = MainView.class)
@ViewController("exp_PersonalView")
@ViewDescriptor("personal-view.xml")
public class PersonalView extends StandardView {
    @ViewComponent
    private InstanceContainer<User> userDc;
    @Autowired
    private CurrentAuthentication currentAuthentication;
    @Autowired
    private DataManager dataManager;
    @Autowired
    private Notifications notifications;
    @Autowired
    private ViewNavigators viewNavigators;

    @Subscribe
    public void onReady(final ReadyEvent event) {
        userDc.setItem((User) currentAuthentication.getUser());
    }

    @Subscribe(id = "saveAndCloseBtn", subject = "clickListener")
    public void onSaveAndCloseBtnClick(final ClickEvent<JmixButton> event) {
        dataManager.save(userDc.getItem());
        notifications.create("Saved successfully!").withType(Notifications.Type.SUCCESS).show();
        viewNavigators.view(this, MainView.class).navigate();
    }

    @Subscribe(id = "closeBtn", subject = "clickListener")
    public void onCloseBtnClick(final ClickEvent<JmixButton> event) {
        viewNavigators.view(this, MainView.class).navigate();
    }
}